//------------------------------------------------------------------------------
// X2TK Graphic Toolkit
// Copyright (C) 2006-2008 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#include "x2tk_Container.h"
#include "pmsg.h"

namespace x2tk {

Container::Container(int x, int y, int w, int h) : Widget(x,y,w,h) {
  manageFocus_=false;
  keepFocus_=false;
}

Container::~Container() { }

void Container::draw(GraphicContext &g) {
  Widget::draw(g);
  for(int i=0; i<nChildren(); i++) {
    Widget *w = (*this)[i];
    if(w->isVisible()) {
      GraphicContext gPrime = g;
      gPrime.limit(w->getX(), w->getY(), w->getW(), w->getH());
      w->draw(gPrime);
    }
  }
}

// Mouse down event is sent to all(!) the objects inside the container
// that are visible.
bool Container::mouseDown(int type) {
  bool wanted=false;
  
  for(int i=nChildren(); i--;) {
    if((*this)[i]->isVisible()) {
      wanted = (*this)[i]->mouseDown(type);
      if(wanted) break;
    }
  }
  return wanted;
}

// mouseUp event is sent to all(!) the objects inside the container
// that are cisible.
void Container::mouseUp(int type) {
  for(int i=nChildren(); i--;) {
    if((*this)[i]->isVisible()) (*this)[i]->mouseUp(type);
  }
}

// This event is sent to all(!) the widgets inside the container that are
// visible. The position of the mouse is relative.
void Container::mouseMove(int x, int y) {
  for(int i=nChildren(); i--;) {
    if((*this)[i]->isVisible()) {
      int wx=(*this)[i]->getX();
      int wy=(*this)[i]->getY();
      (*this)[i]->mouseMove(x-wx, y-wy); 
    }
  }
}

void Container::resize(int w, int h) {
  Widget::resize(w,h);
  for(int i=nChildren();i--;) {
    (*this)[i]->layout();
  }
}

} // namespace x2tk
